package com.idbk.chargestation.activity.user;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.text.InputFilter;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonUser;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GlobalResult;
import com.idbk.chargestation.util.GsonUtils;

/**
 * 用户信息编辑
 *
 * @author lupc, zhx, lupc
 */
public class ActivityUserInforSet extends BaseActivity implements OnClickListener {

    private final static String TAG = ActivityUserInforSet.class.getSimpleName();

    public final static String KEY_EDIT_INDEX = "key_edit_index";

    public final static int INDEX_USER_NICK_NAME = 0;
    public final static int INDEX_USER_PHONE = 1;
    public final static int INDEX_USER_ADDRESS = 2;
    public final static int INDEX_USER_ZIP = 3;
    public final static int INDEX_USER_CARD = 4;
    public final static int INDEX_USER_SIGNATURE = 5;

    private TextInputLayout mTextInputLayout;
    private EditText mEditInput;

    private JsonUser mUser;

    public final static String[] mTitles = {
            "昵称",
            "移动电话",
            "地址",
            "邮编",
            "身份证",
            "个性签名",
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_user_infor_set);

        setupView();

        setupData();
    }

    //初始化界面
    @SuppressWarnings("ConstantConditions")
    private void setupView() {
        setupToolBar2();//显示标题栏

        //初始化控件
        mTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout_input);
        mEditInput = (EditText) findViewById(R.id.edittext_input);

        findViewById(R.id.textview_submit).setOnClickListener(this);
    }

    private void setupData() {
        mUser = getIntent().getParcelableExtra(JsonUser.KEY);
        if (mUser == null) {
            Toast.makeText(this, "参数错误！", Toast.LENGTH_SHORT).show();
            finish();
        }

        int index = getIntent().getIntExtra(KEY_EDIT_INDEX, -1);
        switch (index) {
            case INDEX_USER_NICK_NAME:
                setToolbar2Title(mTitles[index]);
                mTextInputLayout.setHint(mTitles[index]);
                mEditInput.setHint(mTitles[index]);
                //
                setupUserNickName();
                break;
            case INDEX_USER_PHONE:
                setToolbar2Title(mTitles[index]);
                mTextInputLayout.setHint(mTitles[index]);
                mEditInput.setHint(mTitles[index]);
                //
                setupUserPhone();
                break;
            case INDEX_USER_ADDRESS:
                setToolbar2Title(mTitles[index]);
                mTextInputLayout.setHint(mTitles[index]);
                mEditInput.setHint(mTitles[index]);
                //
                setupUserAddress();
                break;
            case INDEX_USER_ZIP:
                setToolbar2Title(mTitles[index]);
                mTextInputLayout.setHint(mTitles[index]);
                mEditInput.setHint(mTitles[index]);
                //
                setupUserZip();
                break;
            case INDEX_USER_CARD:
                setToolbar2Title(mTitles[index]);
                mTextInputLayout.setHint(mTitles[index]);
                mEditInput.setHint(mTitles[index]);
                //
                setupUserCardNo();
                break;
            case INDEX_USER_SIGNATURE:
                setToolbar2Title(mTitles[index]);
                mTextInputLayout.setHint(mTitles[index]);
                mEditInput.setHint(mTitles[index]);
                //
                setupUserSignature();
                break;
            default:
                break;
        }
    }

    private void setupUserNickName() {
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(11);
        mEditInput.setFilters(fArray);

        mEditInput.setText(mUser.realName == null ? "" : mUser.realName);
    }

    private void setupUserPhone() {
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(11);
        mEditInput.setFilters(fArray);
        mEditInput.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

        mEditInput.setText(mUser.mobile == null ? "" : mUser.mobile);
    }

    private void setupUserAddress() {
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(30);
        mEditInput.setFilters(fArray);

        mEditInput.setText(mUser.address == null ? "" : mUser.address);
    }

    private void setupUserZip() {
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(6);
        mEditInput.setFilters(fArray);
        mEditInput.setKeyListener(DigitsKeyListener.getInstance("0123456789"));

        mEditInput.setText(mUser.zip == null ? "" : mUser.zip);
    }

    private void setupUserCardNo() {
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(18);
        mEditInput.setFilters(fArray);
        mEditInput.setKeyListener(DigitsKeyListener.getInstance("0123456789Xx"));

        mEditInput.setText(mUser.cardNo == null ? "" : mUser.cardNo);
    }

    private void setupUserSignature() {
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(30);
        mEditInput.setFilters(fArray);

        mEditInput.setText(mUser.signature == null ? "" : mUser.signature);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textview_submit:
                submit();
                break;
            default:
                break;
        }
    }

    private void submit() {

        int index = getIntent().getIntExtra(KEY_EDIT_INDEX, -1);
        switch (index) {
            case INDEX_USER_NICK_NAME:
                String nickName = mEditInput.getText().toString();
                if (nickName.equals("") || nickName.length() < 2) {
                    Snackbar.make(mEditInput, "昵称长度必须为2个字符以上", Snackbar.LENGTH_SHORT).show();
                    return;
                } else if (containsEmoji(nickName)) {
                    Snackbar.make(mEditInput, "昵称不可含有表情符号", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                mUser.realName = nickName;
                break;
            case INDEX_USER_PHONE:
                String phoneNum = mEditInput.getText().toString();
                if (phoneNum.equals("") && phoneNum.length() != 11 && phoneNum.length() != 0) {
                    Snackbar.make(mEditInput, "手机号码必须为11位数字", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                mUser.mobile = phoneNum;
                break;
            case INDEX_USER_ADDRESS:
                String address = mEditInput.getText().toString();
                if (address.equals("")) {
                    if (containsEmoji(address)) {
                        Snackbar.make(mEditInput, "地址不可含有表情符号", Snackbar.LENGTH_SHORT).show();
                        return;
                    }
                }
                mUser.address = address;
                break;
            case INDEX_USER_ZIP:
                String zip = mEditInput.getText().toString();
                if (zip.equals("") && zip.length() != 6 && zip.length() != 0) {
                    Snackbar.make(mEditInput, "邮政编码必须为6位数字", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                mUser.zip = zip;
                break;
            case INDEX_USER_CARD:
                String cardNo = mEditInput.getText().toString();
                //身份证号码
                if (cardNo.equals("") && cardNo.length() != 18 && cardNo.length() != 0) {
                    Snackbar.make(mEditInput, "身份证号码必须为18位", Snackbar.LENGTH_SHORT).show();
                    return;
                }
                mUser.cardNo = cardNo;
                break;
            case INDEX_USER_SIGNATURE:
                mUser.signature = mEditInput.getText().toString();
                break;
            default:
                break;
        }

        //显示加载框
        showMyProgressDialog(true, false, "提交中...");
        //设置个人信息
        mMainRequest = ChargeStationAPI.setUserInfo(
                mUser.realName,
                mUser.signature,
                mUser.mobile,
                mUser.address,
                mUser.zip,
                mUser.cardNo,
                mResponse);
    }

    private final EHttpResponse mResponse = new EHttpResponse() {

        @Override
        public void onEResponse(String response) {
            JsonUser result = GsonUtils.toBean(JsonUser.class, response);//解析结果
            if (result.status == GlobalResult.OK.getStatus()) {
                setResult(RESULT_OK);
                showResult(ActivityUserInforSet.this, "提交成功!", true);
            }
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            //打印调试信息
            Log.e(TAG, error.getMessage(), error.getCause());
            Snackbar.make(mEditInput, R.string.common_tip_submit_failed, Snackbar.LENGTH_SHORT).show();
        }

        @Override
        public void onEFinish() {
            //关闭 等待框
            super.onEFinish();
            dismissMyProgressDialog();
        }
    };

}
